package com.splant.android.smart.UILevel.Community;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.splant.android.R;
import com.splant.android.smart.ModelLevel.DetectorListBean;
import com.splant.android.smart.ModelLevel.event.AddPageEvent;
import com.splant.android.smart.UILevel.Community.fragments.Garden_DetectorDatas_Fragment;
import com.splant.android.smart.UILevel.System_ViewPagerStateFragment_Adapter;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class Garden_Detector_Activity extends FragmentActivity implements View.OnClickListener {


    private LinearLayout ll_soilTemperature;
    private LinearLayout ll_soilHumidity;
    private LinearLayout ll_soilNutrient;
    private DetectorListBean.Detector detector;
    private TextView tv_detectorName;
    private ImageView img_plant;
    private TextView tv_plantName;
    private ViewPager vp_detectorDatas;
    private Button btn_goBack;

    private TextView tv_tempture;
    private ImageView img_soilHumidity0;
    private ImageView img_soilHumidity1;
    private ImageView img_soilHumidity2;
    private ImageView img_soilHumidity3;
    private ImageView img_soilHumidity4;

    private ImageView img_soilNutrien0;
    private ImageView img_soilNutrien1;
    private ImageView img_soilNutrien2;
    private ImageView img_soilNutrien3;
    private ImageView img_soilNutrien4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.garden_detector_activity);

        btn_goBack = (Button) findViewById(R.id.btn_goBack);
        tv_detectorName = (TextView) findViewById(R.id.tv_detectorName);
        img_plant = (ImageView) findViewById(R.id.img_plant);
        tv_plantName = (TextView) findViewById(R.id.tv_plantName);
        tv_tempture = (TextView) findViewById(R.id.tv_tempture);

        img_soilHumidity0 = (ImageView) findViewById(R.id.img_soilHumidity0);
        img_soilHumidity1 = (ImageView) findViewById(R.id.img_soilHumidity1);
        img_soilHumidity2 = (ImageView) findViewById(R.id.img_soilHumidity2);
        img_soilHumidity3 = (ImageView) findViewById(R.id.img_soilHumidity3);
        img_soilHumidity4 = (ImageView) findViewById(R.id.img_soilHumidity4);

        img_soilNutrien0 = (ImageView) findViewById(R.id.img_soilNutrien0);
        img_soilNutrien1 = (ImageView) findViewById(R.id.img_soilNutrien1);
        img_soilNutrien2 = (ImageView) findViewById(R.id.img_soilNutrien2);
        img_soilNutrien3 = (ImageView) findViewById(R.id.img_soilNutrien3);
        img_soilNutrien4 = (ImageView) findViewById(R.id.img_soilNutrien4);

        ll_soilTemperature = (LinearLayout) findViewById(R.id.ll_soilTemperature);
        ll_soilHumidity = (LinearLayout) findViewById(R.id.ll_soilHumidity);
        ll_soilNutrient = (LinearLayout) findViewById(R.id.ll_soilNutrient);
        vp_detectorDatas = (ViewPager) findViewById(R.id.vp_paraDatas);

        init();

        btn_goBack.setOnClickListener(this);
        ll_soilTemperature.setOnClickListener(this);
        ll_soilHumidity.setOnClickListener(this);
        ll_soilNutrient.setOnClickListener(this);

        EventBus.getDefault().register(this);
    }

    private System_ViewPagerStateFragment_Adapter mViewPagerFragmentAdapter;
    private List<Fragment> mList = new ArrayList<Fragment>();

    private void init() {

        detector = (DetectorListBean.Detector) getIntent().getSerializableExtra("detector");
        if (detector == null)
            return;

        tv_detectorName.setText(detector.getDetectorName());

        Glide.with(this)
                .load(detector.getPlantImgUrl())
                .dontAnimate()
                .placeholder(R.mipmap.garden_ic_plantimg_fail)
                .into(img_plant);

        tv_plantName.setText(detector.getPlantName());
        tv_tempture.setText(detector.getTemperature() + "℃");

        if (detector.getHumidity().equals("1")) {
            img_soilHumidity0.setImageResource(R.mipmap.garden_ic_detector_soilhumidity_abnormal);
            img_soilHumidity1.setImageResource(R.mipmap.garden_ic_detector_soilhumidity_abnormal);
            img_soilHumidity0.setVisibility(View.VISIBLE);
            img_soilHumidity1.setVisibility(View.VISIBLE);
        } else if (detector.getHumidity().equals("2")) {
            img_soilHumidity0.setVisibility(View.VISIBLE);
            img_soilHumidity1.setVisibility(View.VISIBLE);
            img_soilHumidity2.setVisibility(View.VISIBLE);
        } else if (detector.getHumidity().equals("3")) {
            img_soilHumidity0.setVisibility(View.VISIBLE);
            img_soilHumidity1.setVisibility(View.VISIBLE);
            img_soilHumidity2.setVisibility(View.VISIBLE);
            img_soilHumidity3.setVisibility(View.VISIBLE);
            img_soilHumidity4.setVisibility(View.VISIBLE);
        }

        if (detector.getSoilNutrient().equals("1")) {
            img_soilNutrien0.setImageResource(R.mipmap.garden_ic_detector_soilnutrient_abnormal);
            img_soilNutrien1.setImageResource(R.mipmap.garden_ic_detector_soilnutrient_abnormal);
            img_soilNutrien0.setVisibility(View.VISIBLE);
            img_soilNutrien1.setVisibility(View.VISIBLE);
        } else if (detector.getSoilNutrient().equals("2")) {
            img_soilNutrien0.setVisibility(View.VISIBLE);
            img_soilNutrien1.setVisibility(View.VISIBLE);
            img_soilNutrien2.setVisibility(View.VISIBLE);
        } else if (detector.getSoilNutrient().equals("3")) {
            img_soilNutrien0.setVisibility(View.VISIBLE);
            img_soilNutrien1.setVisibility(View.VISIBLE);
            img_soilNutrien2.setVisibility(View.VISIBLE);
            img_soilNutrien3.setVisibility(View.VISIBLE);
            img_soilNutrien4.setVisibility(View.VISIBLE);
        }

        if (detector.getAbnState() == 0)
            tv_plantName.setBackgroundResource(R.color.garden_plant_normal);
        else
            tv_plantName.setBackgroundResource(R.color.garden_plant_abnormal);

        mList.add(new Garden_DetectorDatas_Fragment(detector,0, 0));

        FragmentManager mFragmentManager = getSupportFragmentManager();
        mViewPagerFragmentAdapter = new System_ViewPagerStateFragment_Adapter(mFragmentManager, mList);

        vp_detectorDatas.addOnPageChangeListener(new ViewPagetOnPagerChangedLisenter());
        vp_detectorDatas.setAdapter(mViewPagerFragmentAdapter);

    }
    private int paraType;
    public void onEventMainThread(AddPageEvent addPageEvent)
    {

        Log.i("Detector","添加页面");

        mList.add(new Garden_DetectorDatas_Fragment(detector,paraType,mList.size()));

        mViewPagerFragmentAdapter.notifyDataSetChanged();

        vp_detectorDatas.setCurrentItem(mList.size()-2);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_goBack:
                finish();
                break;
            case R.id.ll_soilTemperature:

                ll_soilTemperature.setBackgroundResource(R.color.garden_color1);
                ll_soilHumidity.setBackgroundResource(R.color.garden_color0);
                ll_soilNutrient.setBackgroundResource(R.color.garden_color0);

                paraType = 0;
                selectParamter();
                break;
            case R.id.ll_soilHumidity:

                ll_soilTemperature.setBackgroundResource(R.color.garden_color0);
                ll_soilHumidity.setBackgroundResource(R.color.garden_color1);
                ll_soilNutrient.setBackgroundResource(R.color.garden_color0);
                paraType = 1;
                selectParamter();
                break;
            case R.id.ll_soilNutrient:

                ll_soilTemperature.setBackgroundResource(R.color.garden_color0);
                ll_soilHumidity.setBackgroundResource(R.color.garden_color0);
                ll_soilNutrient.setBackgroundResource(R.color.garden_color1);
                paraType = 2;
                selectParamter();
                break;
        }
    }

    private void selectParamter()
    {
        for (Fragment fragment:mList)
        {
            ((Garden_DetectorDatas_Fragment)fragment).selectedPara(paraType);
        }
    }

    private final int MAX_DAY_COUNT=10;
    private int curPageNo;
    private class ViewPagetOnPagerChangedLisenter implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }
        @Override
        public void onPageSelected(int position) {

            if (position<MAX_DAY_COUNT)
            {
                if (position>curPageNo&&position==mList.size()-1)
                {
                    Log.i("Detector","加载数据");
                    ((Garden_DetectorDatas_Fragment) mList.get(position)).getDatas();
                }

            }

            Log.i("Detector","页面变更");

            if (position==0&&position<curPageNo)
            {
                Toast.makeText(Garden_Detector_Activity.this,"已经到达最前数据了",Toast.LENGTH_SHORT).show();
            }

            curPageNo=position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
    @Override
    public void onDestroy()
    {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
